﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace AuthenticationService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AuthenticationSrv" in code, svc and config file together.
    public class AuthenticationSrv : IAuthenticationSrv
    {
        public List<byte[]> getAllPictures()
        {
                using (var db = new questEntities())
                {
                    return db.Authentication_pics.Select(p => p.picture).ToList();
                }
        }

        public int authenticate(string name, string schoolName, string type, int pic1, int pic2)
        {
            using (var db = new questEntities())
            {
                System.Linq.IQueryable<int> query;
                int pk_user = 0;
                try
                {
                    if (type == "student")
                    {

                        query = db.Users.Where(p => (p.first_name.Equals(name,StringComparison.CurrentCultureIgnoreCase)) && (p.Student.School.school_name.Equals(schoolName,StringComparison.CurrentCultureIgnoreCase)) &&
                                                   (p.Student.fk_pic1_id == pic1) && (p.Student.fk_pic2_id == pic2)).Select(p => p.pk_user_id);
                        pk_user = query.ToList<int>().First();
                    }
                    if (type == "teacher")
                    {

                        query = db.Users.Where(p => (p.first_name.Equals(name, StringComparison.CurrentCultureIgnoreCase)) && (p.Teacher.School.school_name.Equals(schoolName, StringComparison.CurrentCultureIgnoreCase)) &&
                                                   (p.Teacher.fk_pic1_id == pic1) && (p.Teacher.fk_pic2_id == pic2)).Select(p => p.pk_user_id);
                        pk_user = query.ToList<int>().First();
                    }
                    return pk_user;
                }

                catch
                {
                    return 0;
                }
            }
        }
    }
}
